//
//  ViewController.m
//  CoreMLDemo
//
//  Created by WTFKL on 2017/10/10.
//  Copyright © 2017年 WTF. All rights reserved.
//

#import "ViewController.h"
#import "GoogLeNetPlaces.h"
#import "UIImage+Extension.h"
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}



- (IBAction)takePhoto:(id)sender {
    UIImagePickerController * pickerVc = [[UIImagePickerController alloc] init];
    pickerVc.sourceType = UIImagePickerControllerSourceTypeCamera;
    pickerVc.delegate = self;
    
    [self presentViewController:pickerVc animated:YES completion:nil];
}

- (IBAction)confirmPicture:(id)sender {
    
    NSString * test = [self predictImageScene:self.showImage.image];
    self.areaLabel.text = test;
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    //获取到的图片
    UIImage * image = [info valueForKey:UIImagePickerControllerOriginalImage];
    self.showImage.image = image;
}

-(NSString *)predictImageScene:(UIImage *)image{
    GoogLeNetPlaces *model = [[GoogLeNetPlaces alloc] init];
    NSError *error;
    UIImage *scaledImage = [image scaleToSize:CGSizeMake(224, 224)];
    CVPixelBufferRef buffer = [image pixelBufferFromCGImage:scaledImage];
    GoogLeNetPlacesInput *input = [[GoogLeNetPlacesInput alloc] initWithSceneImage:buffer];
    GoogLeNetPlacesOutput *output = [model predictionFromFeatures:input error:&error];
    NSLog(@"error---------%@",error);
    
    return output.sceneLabel;
}
@end
